package com.wanmait.schedule.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Department;
import com.wanmait.schedule.entity.Project;
import com.wanmait.schedule.service.*;
import com.wanmait.schedule.entity.*;
import com.wanmait.schedule.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Stack;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/project")
@Slf4j
public class ProjectController {
    @Resource
    private  ProjectService projectService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private CheckService checkService;
    @Resource
    private CompanyUserService companyUserService;
    @Resource
    private TaskService taskservice;
    @Resource
    private StateService stateService;

    /**
     * 通过企业用户查询所有的项目
     */
    @GetMapping("member/findProject/{id}")
    public Result findProject(@PathVariable Integer id){
      return Result.success(projectService.findAllProject(id));
    }
    //------------------------------------------------
    /*部长功能*/
        /**部长查看分配的项目,分页
    * */
    @RequestMapping("/minister/selectProject")
    public Result selectProject(Integer pageSize, Integer pageNum, Integer companyUserId){
        Page<Project> projectPage = projectService.selectProjectByPage(pageSize,pageNum,companyUserId);
        return Result.success("selectOK",projectPage);
    }
    /*
     * 部长接收项目后进行分配任务 。修改check表,表示已查看项目
     * */
    @RequestMapping("/minister/acceptProject")
    public Result acceptProject(Integer projectId){
        System.out.println("contro:"+projectId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("check",checkService.receiveProject(projectId));
        return Result.success("HasCheckedProject",jsonObject);
    }
    /**部长查看项目详情详情
    * */
    @RequestMapping("/minister/projectDetail")
    public Result selectOneProject(Integer projectId){
        System.out.println(projectId);
        Project project = projectService.selectOneProjectById(projectId);
        return  Result.success(project);
    }
    /**部长将项目进行分配，分配给组员任务  参数：分配任务人id，接收任务人id，企业，项目，部门id；
     * 给哪位组员分配何种任务
    * */
    @PostMapping("/minister/assignTask")
    public Result assignTask(@RequestBody Task task){
        System.out.println("task————————:"+task);
        taskservice.addTask(task);
        return Result.success("AddDone");
    }
    /**获取部门成员列表：成员架构
     * */
    @RequestMapping("/minister/getMembers")
    public Result getMembers(HttpServletRequest request){
        CompanyUser companyUser = (CompanyUser)request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        Integer deptId = companyUser.getDepartmentId();
        log.info("进入getMEnbers");
        return Result.success(companyUserService.selectCompanyUsersOfDep(companyId,deptId));
    }
    /**部长查看没有提交的项目，用于提交申请
     * */
   /* @RequestMapping("/minister/noSubmitProject")
    public Result getNoSubmitProjects(HttpServletRequest request,String keyword){
        System.out.println("keyword"+keyword);
        System.out.println(keyword=="");
        CompanyUser companyUser = (CompanyUser)request.getAttribute("companyUser");
        Integer companyUserId = companyUser.getId();
        System.out.println("id_"+companyUserId);
        if(keyword==""){
            return Result.success(projectService.selectNoSubmitProjects2(companyUserId));
        }
        return  Result.success( projectService.selectNoSubmitProjects(companyUserId,keyword));

    }*/
    /*部长将全部审核完的任务 ，进行项目提交（必须是全部审核通过才可以提交项目）
         1：检查这位部长这个项目  所属下的任务是否全部完成，没有完成不能提交
         2：全部审核通过后  进行提交 submit =1
         * */
    @RequestMapping("/minister/submitProject")
    public  Result submit(Integer companyUserId,Integer projectId,HttpServletRequest request){
//        验证一下传过来的companyUserId 是否是同一个人
        Integer id = ((CompanyUser)request.getAttribute("companyUser")).getId();
        if(companyUserId != id ){
            return Result.error("人员信息错误");
        }
        Boolean isAllPass = projectService.submitProject(projectId);
        System.out.println("projectID--"+projectId);
        if(isAllPass){
            return Result.success("可以提交");
        }
        return Result.error("不可提交");
    }

    /*部长查看驳回表中的项目
       驳回：驳回表中的项目
    * */
    @RequestMapping("/minister/rejectProjectCheck")
    public  Result rejectProjectCheck(HttpServletRequest request) {
//        项目Id，任务Id 两类  部长项目提交驳回 可以重新提交； 部长任务提交驳回
        CompanyUser companyUser = (CompanyUser) request.getAttribute("companyUser");
        System.out.println(companyUser);
//        Integer companyId = companyUser.getCompanyId();
//        Integer deptId = companyUser.getDepartmentId();
        Integer companyUserId = companyUser.getId();
        return Result.success(projectService.rejectProjectList(companyUserId));
    }
    @RequestMapping("/minister/rejectTaskCheck")
    public Result rejectTaskCheck(HttpServletRequest request){
        return null;
    }

    /*部长了解到驳回表的内容之后，将不通过的的项目重新分配给  组内成员
        任务表 中增加了组内成员的任务 -->这个项目变为不可提交状态（因为存在没有完成的任务）
    * */
// ---------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------
    /*老板功能*/
    /**老板分配项目
     * */
    @PostMapping("leader/release")
    public Result release(@RequestBody Project project,HttpServletRequest request){
        System.out.println("project"+project);
        CompanyUser companyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        project.setCompanyId(companyId);
        boolean flag = projectService.release(project);
        if (flag){
            return Result.success("发布成功");
        }
        else {
            return Result.error("发布失败");
        }
    }
    @GetMapping("leader/initProject")
    public Result initProject(Integer pageNum, Integer pageSize,HttpServletRequest request){
        CompanyUser companyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        PageInfo<Project> pageInfo = projectService.findPage(pageNum,pageSize,companyId);
        return Result.success(pageInfo);
    }
    @GetMapping("leader/editInitData/{id}")
    public Result editInitData(@PathVariable Integer id){
        Project project = projectService.getById(id);
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getId,project.getDepartmentId());
        Department department = departmentService.getOne(queryWrapper);
        project.setDepartment(department);
        return Result.success(project);
    }
    @PostMapping("leader/editProject")
    public Result editProject(@RequestBody Project project){
        projectService.updateProjectFindById(project);
        return Result.success("成功");
    }
    @GetMapping("leader/projectFindAll")
    public Result projectFindAll(Integer pageNum,Integer pageSize,HttpServletRequest request){
        CompanyUser companyUser =(CompanyUser)request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        PageInfo<Project> projects = projectService.projectFindAll(pageNum,pageSize,companyId);
        return Result.success(projects);
    }
    @GetMapping("leader/initDepartment")
    public Result initDepartment(HttpServletRequest request){
        CompanyUser companyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        List<Department> departments = departmentService.initDepartment(companyId);
        return Result.success(departments);
    }
    /*项目跟踪表下每个项目下的子任务
    * */
    @GetMapping("leader/taskList/{id}")
    public Result taskList(@PathVariable Integer id){
        List<Task> taskList = projectService.taskList(id);
        return Result.success(taskList);
    }
    /*ES搜索
    * */
    @GetMapping("leader/search")
    public Result searchProject(String searchContent){
        List<Project> projects = projectService.search(searchContent);
        return Result.success(projects);
    }
    /**给个人发布项目时读取企业用户
    * */
    @GetMapping("leader/initCompanyUser")
    public Result initCompanyUser(HttpServletRequest request){
        CompanyUser companyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        List<CompanyUser> companyUserList = projectService.initCompanyUser(companyId);
        return Result.success(companyUserList);
    }


}
